﻿
Imports std

Partial Public Class fs
    Private Delegate Function find_sub_node_d(ByVal n As node,
                                              ByVal name As String,
                                              ByVal p As pointer(Of node)) As eventComb

    Private Shared Function find_node(ByVal path As String,
                                      ByVal node As pointer(Of node),
                                      ByVal f As find_sub_node_d) As eventComb
        assert(Not f Is Nothing)
        Dim paths As vector(Of String) = Nothing
        Dim i As Int64 = 0
        Dim ec As eventComb = Nothing
        Dim n As node = Nothing
        Return New eventComb(Function() As Boolean
                                 If split_path(path, paths) Then
                                     n = root
                                     If node Is Nothing Then
                                         node = New pointer(Of node)()
                                     End If
                                     Return goto_next()
                                 Else
                                     Return False
                                 End If
                             End Function,
                             Function() As Boolean
                                 If i < paths.size() Then
                                     ec = f(n, paths(i), node)
                                     Return waitfor(ec) AndAlso
                                            goto_next()
                                 Else
                                     Return goto_end()
                                 End If
                             End Function,
                             Function() As Boolean
                                 If ec.end_result() Then
                                     n = +node
                                     assert(Not n Is Nothing)
                                     i += 1
                                     Return goto_prev()
                                 Else
                                     Return False
                                 End If
                             End Function)
    End Function

    Public Shared Function find_node(ByVal path As String,
                                     ByVal node As pointer(Of node)) As eventComb
        Return find_node(path, node,
                         Function(n As node, name As String, p As pointer(Of node)) As eventComb
                             Return n.find_sub_node(name, p)
                         End Function)
    End Function

    Public Shared Function create_node(ByVal path As String,
                                       ByVal node As pointer(Of node)) As eventComb
        Return find_node(path, node,
                         Function(n As node, name As String, p As pointer(Of node)) As eventComb
                             Return n.find_create_sub_node(name, p)
                         End Function)
    End Function

    Public Shared Function split_path(ByVal path As String, ByRef output As vector(Of String)) As Boolean
        If isemptystring(path) Then
            Return False
        Else
            strsepar2(path, constants.path_separator, output, True)
            Return True
        End If
    End Function
End Class
